package fr.umlv.m2.jee.spij.service.partial;

import fr.umlv.m2.jee.spij.persistence.education.Partial;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Date;

/**
 * @author thomas magny
 * @author mathieu mazzolini
 */
@Component("partialBean")
public class DisplayPartial implements Serializable {

    /**
     * Primary key.
     */
    private long id;

    /**
     * About teaching.
     */
    private String subject;

    /**
     * Partial's Correction.
     */
    private String correction;

    /**
     * Link of Partial's Correction in PDF.
     */
    private String linkCorrection;

    /**
     * Partial's date.
     */
    private Date date;

    /**
     * Partial's start time.
     */
    private Date startTime;

    /**
     * Partial's end time.
     */
    private Date endTime;

    /**
     * Consultation's date. The date is null if no consultation is expected.
     */
    private Date consultationDate;

    /**
     * Consultation's hour. The hour is null if no consultation is expected.
     */
    private Date consultationHour;

    /**
     * Id of linked unit.
     */
    private Long unit;

    /**
     * The unit name
     */
    private String unitName;

    /**
     * Weight of the review
     */
    private int weight;

    /**
     * Empty constructor.
     */
    public DisplayPartial() {

    }

    /**
     * Constructs a DisplayPartial from the partial given in argument
     *
     * @param partial the partial to display
     */
    public DisplayPartial(Partial partial) {
        if (partial != null)
            storeData(partial);
    }

    /**
     * @return the id
     */
    public long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * @return the subject
     */
    public String getSubject() {
        return subject;
    }

    /**
     * @param subject the subject to set
     */
    public void setSubject(String subject) {
        this.subject = subject;
    }

    /**
     * @return the correction
     */
    public String getCorrection() {
        return correction;
    }

    /**
     * @param correction the correction to set
     */
    public void setCorrection(String correction) {
        this.correction = correction;
    }

    /**
     * @return the linkCorrection
     */
    public String getLinkCorrection() {
        return linkCorrection;
    }

    /**
     * @param linkCorrection the linkCorrection to set
     */
    public void setLinkCorrection(String linkCorrection) {
        this.linkCorrection = linkCorrection;
    }

    /**
     * @return the date
     */
    public Date getDate() {
        return new Date(date.getTime());
    }

    /**
     * @param date the date to set
     */
    public void setDate(Date date) {
        this.date = new Date(date.getTime());
    }

    /**
     * @return the startTime
     */
    public Date getStartTime() {
        return new Date(startTime.getTime());
    }

    /**
     * @param startTime the startTime to set
     */
    public void setStartTime(Date startTime) {
        this.startTime = new Date(startTime.getTime());
    }

    /**
     * @return the endTime
     */
    public Date getEndTime() {
        return new Date(endTime.getTime());
    }

    /**
     * @param endTime the endTime to set
     */
    public void setEndTime(Date endTime) {
        this.endTime = new Date(endTime.getTime());
    }

    /**
     * @return the consultationDate
     */
    public Date getConsultationDate() {
        return consultationDate;
        // return new Date(consultationDate.getTime());
    }

    /**
     * @param consultationDate the consultationDate to set
     */
    public void setConsultationDate(Date consultationDate) {
        this.consultationDate = consultationDate;
    }

    /**
     * @return the consultationHour.
     */
    public Date getConsultationHour() {
        return consultationHour;
        /*
        if (consultationHour != null) {
            return new Date(consultationHour.getTime());
        }
    */
    }

    /**
     * @param consultationHour the consultationHour to set.
     */
    public void setConsultationHour(Date consultationHour) {
        this.consultationHour = consultationHour;
    }

    /**
     * @return the unit
     */
    public Long getUnit() {
        return unit;
    }

    /**
     * @param unit the unit to set
     */
    public void setUnit(Long unit) {
        this.unit = unit;
    }

    /**
     * @return the unitName
     */
    public String getUnitName() {
        return unitName;
    }

    /**
     * @param unitName the unitName to set
     */
    public void setUnitName(String unitName) {
        this.unitName = unitName;
    }

    /**
     * @return the weight
     */
    public int getWeight() {
        return weight;
    }

    /**
     * @param weight the weight to set
     */
    public void setWeight(int weight) {
        this.weight = weight;
    }

    private void storeData(Partial partial) {
        if (partial.getId() > 0)
            setId(partial.getId());
        if (partial.getUnit() > 0)
            setUnit(partial.getUnit());
        if (!partial.getSubject().isEmpty())
            setSubject(partial.getSubject());
        if (!partial.getCorrection().isEmpty())
            setCorrection(partial.getCorrection());
        if (!partial.getLinkCorrection().isEmpty())
            setLinkCorrection(partial.getLinkCorrection());
        if (partial.getWeight() > 0)
            setWeight(partial.getWeight());

        if (partial.getDuration() != null) {
            setDate(partial.getDuration().getStart().toDate());
            setStartTime(partial.getDuration().getStart().toDate());
            setEndTime(partial.getDuration().getEnd().toDate());
        }
        if (partial.getConsultation() != null) {
            setConsultationDate(partial.getConsultation().toDate());
            setConsultationHour(partial.getConsultation().toDate());
        }
    }
}
